package com.wy2.springcloud.sample.rest.controller;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.wy2.springcloud.sample.rest.util.JsonTools;

@RestController
public class HomeController {
	
	private static Logger logger=LoggerFactory.getLogger(HomeController.class);

	@Value("${server.port}")
	private String port;
	
	@GetMapping("/")
	public String index() {
		logger.info("访问了测试接口-"+port);
		return "success:"+port;
	}
	
	@RequestMapping(value= {"/statusGather"},method=RequestMethod.POST)
    public String statusGather(@RequestBody Map<String, Object> attachments) {

        List<Map<String,String>> atts=(List<Map<String, String>>) attachments.get("attachments");
        Map<String,String> message=atts.get(0);
        String text=message.get("text");
        AppInfo appInfo=JsonTools.parse(AppInfo.class, text);
        String str="";
        try {
            str=JsonTools.toString(appInfo, true);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //在这里可以根据服务状态进行预警处理，如：发送短信，邮件等，也可以记录服务的状态，进行一些操作
        logger.info("\n 接收到应用状态监控信息 \n {}",str);
        
        return "success:";
    }
	
	/**
	 * 应用信息
	 * @author wuyy<16349023@qq.com>,nnsword
	 * @date 2018年4月19日上午11:51:27
	 *
	 */
	public static class AppInfo implements Serializable{
	    
	    /**
         * 
         */
        private static final long serialVersionUID = -6408873144086823344L;
        
        private String serviceUrl;
        private String serviceId;
	    private String appId;
	    private String status;
	    
	    
	    
        public String getServiceUrl() {
            return serviceUrl;
        }
        public void setServiceUrl(String serviceUrl) {
            this.serviceUrl = serviceUrl;
        }
        public String getServiceId() {
            return serviceId;
        }
        public void setServiceId(String serviceId) {
            this.serviceId = serviceId;
        }
        public String getAppId() {
            return appId;
        }
        public void setAppId(String appId) {
            this.appId = appId;
        }
        public String getStatus() {
            return status;
        }
        public void setStatus(String status) {
            this.status = status;
        }
	    
	    
	}
}
